package org.csu.pm.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.pm.common.BaseResponse;
import org.csu.pm.common.ResultUtils;
import org.csu.pm.entity.BookInfo;
import org.csu.pm.exception.BusinessException;
import org.csu.pm.mapper.BookInfoMapper;
import org.csu.pm.service.BookService;
import org.springframework.stereotype.Service;


import java.util.List;

import static org.csu.pm.common.ErrorCode.*;

/**
 * @author kwanho
 * @description 针对表【book_info】的数据库操作Service实现
 * @createDate 2022-11-11 20:16:25
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookInfoMapper, BookInfo> implements BookService {

    // 查询预约信息
    @Override
    public BaseResponse<?> getBookInfo(Long id) {
        BookInfo bookInfo = this.query().eq("id", id).one();
        if (bookInfo == null) {
            throw new BusinessException(NULL_ERROR, "查询为空");
        }
        return ResultUtils.success(bookInfo);
    }

    // 查询所有预约信息 List
    @Override
    public BaseResponse<?> getBookInfoList() {
        List<BookInfo> bookInfoList = query().list();
        return ResultUtils.success(bookInfoList);
    }

    // 根据userId查询预约信息
    @Override
    public BaseResponse<?> getBookInfoListByUserId(Long userId) {
        List<BookInfo> bookInfoList = query().eq("user_id", userId).list();
        return ResultUtils.success(bookInfoList);
    }

    // 根据roomId查询预约信息
    @Override
    public BaseResponse<?> getBookInfoListByRoomId(String roomId) {
        List<BookInfo> bookInfoList = query().eq("room_id", roomId).list();
        return ResultUtils.success(bookInfoList);
    }

    // 新增预约信息
    @Override
    public BaseResponse<?> insertBookInfo(BookInfo bookInfo) {
        if (this.save(bookInfo))
            return ResultUtils.success("预约信息添加成功");
        else {
            return ResultUtils.error(SAVE_ERROR, "预约信息添加失败");
        }
    }

    // 删除预约信息
    @Override
    public BaseResponse<?> deleteBookInfo(String id) {
        if (this.getById(id) == null) {
            return ResultUtils.error(NULL_ERROR, "删除的预约信息不存在");
        }
        this.removeById(id);
        return ResultUtils.success("删除预约信息成功");
    }

    // 修改预约信息
    @Override
    public BaseResponse<?> updateBookInfo(String id, BookInfo bookInfo) {
        if (super.getById(id) == null || bookInfo == null || !id.equals(bookInfo.getId().toString())) {
            return ResultUtils.error(NULL_ERROR, "修改的预约信息不存在");
        }
        super.updateById(bookInfo);
        return ResultUtils.success("修改预约信息成功");
    }
}




